//
//  Jumper.swift
//  AG
//
//  Created by zhuxuhong on 2020/11/18.
//  Copyright © 2020 AgoCulture. All rights reserved.
//

import UIKit

public enum Jumper {
    public typealias Completion = BlockVoid
    
    public enum Style {
        case present(
            _ vc: UIViewController,
            animated: Bool = true,
            style: UIModalPresentationStyle = .overFullScreen,
            transition: UIModalTransitionStyle = .coverVertical,
            completion: Completion? = nil
        )
        case push(_ vc: UIViewController,
                  animated: Bool = true)
        case dismissAnimated
        case dismiss(animated: Bool = true,
                     completion: Completion? = nil)
        case popAnimated
        case pop(animated: Bool = true,
                 completion: Completion? = nil)
        case popTo(_ vc: UIViewController,
                   animated: Bool = true,
                   completion: Completion? = nil)
        case openAppSettings
        case openUrl(String?)
    }
    
    public static func jump(_ type: Style) {
        guard let currentVC = Lets.appCurrentVC else {
            fatalError("currentVC为nil!!!!")
        }
        GCD.asyncInMainQueue
        {
            switch type {
            case .present(let vc, let animated, let style, let transition, let completion):
                vc.modalTransitionStyle = transition
                vc.modalPresentationStyle = style
                vc.modalPresentationCapturesStatusBarAppearance = true
                currentVC.present(vc, animated: animated, completion: completion)
            case .push(let vc, let animated):
                currentVC.navigationController?.pushViewController(vc, animated: animated)
            case .dismissAnimated:
                currentVC.dismiss(animated: true, completion: nil)
            case .dismiss(let animated, let completion):
                currentVC.dismiss(animated: animated, completion: completion)
            case .popAnimated:
                currentVC.navigationController?.popViewController(animated: true)
            case .pop(let animated, let completion):
                CATransaction.begin()
                CATransaction.setCompletionBlock(completion)
                currentVC.navigationController?.popViewController(animated: animated)
                CATransaction.commit()
            case .popTo(let vc, let animated, let completion):
                CATransaction.begin()
                CATransaction.setCompletionBlock(completion)
                currentVC.navigationController?.popToViewController(vc, animated: animated)
                CATransaction.commit()
            case .openAppSettings:
                Lets.openUrl(UIApplication.openSettingsURLString)
            case .openUrl(let url):
                Lets.openUrl(url)
            }
        }
    }
}
